package com.example.yeujian5.yuejian5.activity;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.yeujian5.yuejian5.Listenner.UserListener;
import com.example.yeujian5.yuejian5.R;
import com.example.yeujian5.yuejian5.bean.UserBean;
import com.example.yeujian5.yuejian5.model.UserModel;


public class LoginActivity extends AppCompatActivity  implements View.OnClickListener,UserListener{


    private Button btn_reg;
    private Button btn_login;
    private EditText telephone;
    private EditText password;
    private Switch sw_remember;
    private UserBean loginResult;
    SharedPreferences sp;
    String uid,tel,uname;
    String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        btn_login.setOnClickListener(this);
    }

    public void initViews(){
        btn_login=(Button)findViewById(R.id.login_ok);
        btn_reg=(Button)findViewById(R.id.reg_go);
        telephone=(EditText)findViewById(R.id.user);
        password=(EditText)findViewById(R.id.password);
        sw_remember = (Switch)findViewById(R.id.remember);
        sp=getSharedPreferences("loginResult",MODE_PRIVATE);
        readSP();
    }

    public void initEvents(){
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegistActivity.class);
                startActivity(intent);
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (telephone.getText().toString().trim().equals("")||password.getText().toString().trim().equals("")) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                } else{
                    login();
                }

            }
        });
    }
    private void login() {
        UserModel userModel = new UserModel();
        userModel.getLogin(telephone.getText().toString().trim(), password.getText().toString().trim(),this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_ok:
                tel = telephone.getText().toString();
                pwd = password.getText().toString();
                saveSP();
                loginResult = new UserBean();
                UserModel userModel = new UserModel();
                userModel.getLogin(tel, pwd, this);
                break;
        }
    }

    @Override
    public void onResponse(UserBean userBean) {
        Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
        uid=userBean.getId();
        tel=userBean.getTel();
        uname=userBean.getUser_name();
        savaUser(uid,tel,uname);//保存用户id
        Intent intent=new Intent(LoginActivity.this, ContentActivity.class);
        startActivity(intent);
//        finish();
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, "登录失败"+msg, Toast.LENGTH_SHORT).show();
    }

    public  void savaUser(String userid,String tel,String uname){
        SharedPreferences sharedPreferences=getSharedPreferences("Userid", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit=sharedPreferences.edit();
        edit.putString("user_id",userid);
        edit.putString("user_tel",tel);
        edit.putString("user_name",uname);
        edit.commit();
    }

    private void readSP() {
        String uname = sp.getString("username",null);
        String pswd = sp.getString("password",null);
        Boolean remember = sp.getBoolean("remember",false);
        if (remember){
            telephone.setText(uname);
            password.setText(pswd);
            sw_remember.setChecked(remember);
        }
    }

    private void saveSP() {
        SharedPreferences.Editor editor = sp.edit();
        String uname = telephone.getText().toString();
        editor.putString("username",uname);
        String pwd = password.getText().toString();
        editor.putString("password",pwd);
        Boolean remember = sw_remember.isChecked();
        editor.putBoolean("remember",remember);
        editor.commit();
    }

}
